package com.yuwei.module_visit

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.yuwei.http.entity.VisitEntityItem
import com.yuwei.module_visit.databinding.ActivityVisitBinding
import com.yuwei.module_visit.viewmodel.VisitViewModel
import com.yuwei.visit.adapter.VisitAdapter
import com.yuwei.visit.adapter.VisitStatusAdapter

@Route(path = RouterPath.MODULE_VISIT_ACTIVITY)
class VisitActivity : BaseMvvmActivity<ActivityVisitBinding>() {
    private val statusList : List<String> = listOf("全部状态","已通过","驳回","待审核","拒绝")
    private val visitStatusAdapter = VisitStatusAdapter()
    private val visitAdapter = VisitAdapter()
    private var mPosition = 0
    private var visitList = mutableListOf<VisitEntityItem>()
    private val vm: VisitViewModel by viewModels()
    override fun initViews() {
        binding.visitBack.setOnClickListener {
            finish()
        }
        visitStatusAdapter.data.addAll(statusList)
        visitStatusAdapter.notifyDataSetChanged()
        binding.visitStatusRv.adapter = visitStatusAdapter
        binding.visitStatusRv.layoutManager = LinearLayoutManager(this,
            LinearLayoutManager.HORIZONTAL,false)
        visitStatusAdapter.setOnItemClickListener { _, _, position ->
            visitStatusAdapter.setPosition(position)
            mPosition = position
            visitStatusAdapter.notifyDataSetChanged()
            vm.getVisitList()
        }
        binding.visitListRv.adapter = visitAdapter
        binding.visitListRv.layoutManager = LinearLayoutManager(this)
        visitAdapter.setOnItemClickListener { _, _, _ ->
        }
        binding.visitAdd.setOnClickListener {
            ARouter.getInstance().build(RouterPath.MODULE_ADD_VISIT_ACTIVITY).navigation()
        }

    }
    override fun observeDataChange() {
        vm.getVisitListSuccess.observe(this){
            it.reverse()
            visitAdapter.data.clear()
            if(mPosition!=0){
                visitAdapter.data.clear()
                val items = it.filter { item ->
                    item.visitStatus == statusList[mPosition]
                }
                visitAdapter.data.addAll(items)
                visitList.addAll(items)
                visitAdapter.notifyDataSetChanged()
            }else{
                visitAdapter.data.addAll(it)
                visitAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getVisitList()
    }
    override fun getLayoutId(): Int = R.layout.activity_visit

}